<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
//$timing = Array( 'Start of the php file' => microtime() );
ob_start( 'ob_gzhandler()' );
session_start();
header( 'Content-type: text/html; charset=utf-8' );
global $language;
$language = 'en';
include( 'global/dlog.php' );
include( 'configuration/config.php' );
include( 'global/old_database_connection.php');
include( 'global/DatabaseAbstractionLayer.php');
include( 'global/translations.php' );
include( 'global/utils.php' );
$dal = new DatabaseAbstractionLayer();
// Set the database abstraction layer to use the same debug setting as used in the config
$dal->setDebug( DEBUG );

$query = "SELECT gameurl, turns_inactive, turn FROM game";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$game_url = $row[ 'gameurl' ];
$currentTurn = $row[ 'turn' ];
$turnsInactive = $row[ 'turns_inactive' ];
if( strstr( "http://" . $_SERVER[ 'HTTP_HOST' ] . $_SERVER[ 'REQUEST_URI' ], $row[ 'gameurl' ] ) === FALSE ) {
	header( "Location: $game_url" );
	exit();
}
$query5 = "SELECT language, title " .
		"FROM languages " .
		"ORDER BY default_language DESC, title ASC";
$result5 = mysql_query( $query5 ) or die( "Query failed : " . mysql_error() );
$num5 = mysql_numrows( $result5 );
if( isset( $_GET[ 'language' ] ) ) {
	$i = 0;
	while ( $i < $num5 ) {
		if( $_GET[ 'language' ] == mysql_result( $result5, $i, 'language' ) ) {
			$language = mysql_result( $result5, $i, 'language' );
			break;
		}
		$i ++;
	}
}
elseif( isset( $_COOKIE[ 'language' ] ) ) {
	$i = 0;
	while ( $i < $num5 ) {
		if( $_COOKIE[ 'language' ] == mysql_result( $result5, $i, 'language' ) ) {
			$language = mysql_result( $result5, $i, 'language' );
			break;
		}
		$i ++;
	}
}
setcookie( 'language', $language, time() + 31536000, '/' );
//$timing[ 'Start of the HTML' ] = microtime();

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<meta http-equiv="Content-Style-Type" content="text/css" />
		<title>Ages of Strife</title>
		<link type="text/css" rel="stylesheet" href="global/yui/container/assets/container.css"></link>
		<link type="text/css" rel="stylesheet" href="global/agesofstrife.css"></link>
		<meta http-equiv="refresh" content="600"></meta>
	</head>
	<body>
		<div id="root_container">
			<div align="center" id="header-image">
				<a href="index.php?language=<?php print( $language ); ?>">
					<img class="header-dummy-image" src="resources/dummy.gif" alt="Ages of Strife" border="0" />
				</a>
			</div>
<?php
$_SESSION[ 'secret_key' ] = "G5x2FA1s6";
if( isset( $_SESSION[ 'admin' ] ) ) {
	unset( $_SESSION[ 'admin' ] );
}
if( isset( $_SESSION[ 'ruler_id' ] ) ) {
	unset( $_SESSION[ 'ruler_id' ] );
}
if( isset( $_SESSION[ 'user_id' ] ) ) {
	unset( $_SESSION['user_id'] );
}
if( isset( $_SESSION[ 'password' ] ) ) {
	unset( $_SESSION['password'] );
}
include( "global/ads.php" );
?>
			<div id="container">
				<div id="main-content">
					<div class="blok">
						<?php transprint( 'Language' ); ?>:&nbsp;
<?php
	$i = 0;
	while ( $i < $num5 ) {
		if( $i > 0 ) {
			print( ', ' );
		}
		if( $language == mysql_result( $result5, $i, 'language' ) ) {
			print( '<strong>' . mysql_result( $result5, $i, 'title' ) . ' (' . mysql_result( $result5, $i, 'language' ) . ')</strong>' );
		}
		else {
			print( '<a href="' . $game_url . '/index.php?language=' . mysql_result( $result5, $i, 'language' ) . '">' . mysql_result( $result5, $i, 'title' ) . ' (' . mysql_result( $result5, $i, 'language' ) . ')</a>' );
		}
		$i ++;
	}
?>
					</div>
					<br />
<?php
if( isset( $_SESSION[ 'login_status' ] ) ) {
	print( "<div class=\"blok\">" . translate( 'Status' ) . ": " . translate( $_SESSION[ 'login_status' ] ) . "</div><br/>\r\n" );
	unset( $_SESSION[ 'login_status' ] );
}

$query = "SELECT count(ruler_id) 
		FROM ruler
		WHERE active_turn > " . ( $currentTurn - $turnsInactive );
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$totalActiveRulers = $row[ 'count(ruler_id)' ];
?>
					<div id="rightblok">
						<a href="register.php?language=<?php print( $language ); ?>"><strong><?php transprint( 'Register'); ?></strong></a> <?php transprint( 'now' ); ?> <?php transprint( 'to be able to play' ); ?><br />
						<br />
						<?php transprint( 'At' ); ?> <a href="support.php?language=<?php print( $language ); ?>"><?php transprint( 'game rules and support' ); ?></a> <?php transprint( 'you can read about the game rules and recover your username and reset your password.' ); ?><br />
						<br />
						<?php transprint( 'There are' ); ?> <strong><?php print( number_format( $totalActiveRulers ) ); ?></strong> <?php transprint( 'active players at the moment.' ); ?><br />
					</div>
					<form method="post" action="global/login.php">
						<table class="half" id="login-table">
							<tr><th colspan="2"><?php transprint( 'Login' ); ?></th></tr>
							<tr><td><?php transprint( 'Username' ); ?>:</td><td><input name="username" size="18" /></td></tr>
							<tr><td><?php transprint( 'Password' ); ?>:</td><td><input name="password" size="18" type="password" /></td></tr>
							<tr><td>&nbsp;</td><td><input type="submit" value="<?php transprint( 'submit' ); ?>" /><input type="hidden" value="<?php print( $language ); ?>" name="language" /></td></tr>
						</table>
					</form>
					<div class="clear-both-index">&nbsp;</div>
					<div class="blok">
<?php
$query = "SELECT count(*) " .
		"FROM buildings";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$totalBuildings = $row[ 'count(*)' ];
?>
						<div class="army-image-container">
							<a href="register.php?language=<?php print( $language ); ?>" title="Register now" class="sponsor">
								<img src="resources/army-with-text.jpg" alt="Marching army" />
							</a>
						</div>
						<strong><?php transprint( 'Key features' ); ?></strong><br />
						<ul>
							<li><?php transprint( 'Free to play' ); ?></li>
							<li><?php transprint( 'Improve your economy by constructing up to' ); ?> <?php print( $totalBuildings ); ?> <?php transprint( 'different buildings' ); ?></li>
							<li><?php transprint( 'Easy to use ingame chat system' ); ?></li>
							<li><?php transprint( 'Work together as a team to enhance your economy' ); ?></li>
							<li><?php transprint( 'Work together in war by joining allied armies' ); ?></li>
							<li><?php transprint( 'Develop complex strategies to beat your enemies' ); ?></li>
						</ul>
					</div>
					<br />
<?php
$query = "SELECT * " .
		"FROM history " .
		"ORDER BY turn DESC " .
		"LIMIT 1";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$history_title = $row[ 'title' ];
$history_date = $row[ 'date' ];

$query = "SELECT turn, date, max_turn, speed, server_message, spotlight_village_id, spotlight_village_title " .
		"FROM game";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$season = translate( getSeasonString() );
$spotlightVillageId = $row[ 'spotlight_village_id' ];
$spotlightVillageTitle = $row[ 'spotlight_village_title' ];

$date = $row[ 'date' ];
$speed = $row[ 'speed' ];
$speedtext = ( int ) ( $speed / 60 ) . ":";
$speed = ( $speed % 60 );
if( $speed < 10 ) {
	$speed = "0" . $speed;
}
$speedtext = $speedtext . $speed; 

if( $spotlightVillageId == -1 ) {
	$query2 = "SELECT village_id " .
			"FROM village " .
			"WHERE start_place = 1 " .
			"ORDER BY x ASC, y ASC " .
			"LIMIT 1";
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	$spotlightVillageId = $row2[ 'village_id' ];
	$spotlightVillageTitle = '<strong>' . translate( 'Spotlight' ) . '</strong><br />' . translate( 'A Capital' );
}
else {
	$query2 = "SELECT ruler.name AS ruler_name, village.name AS village_name " .
			"FROM village " .
			"JOIN ruler ON ruler.ruler_id = village.ruler_id " .
			"WHERE village_id = $spotlightVillageId";
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC);
	
	if( isset( $row2[ 'ruler_name' ] ) ) {
		$spotlightVillageTitle = '<strong>' . translate( 'Spotlight' ) . '</strong><br />' . translate( $spotlightVillageTitle ) . ':<br />' . $row2[ 'village_name' ] . ' ' . translate( 'controlled by' ) . ' ' . $row2[ 'ruler_name' ];
	}
	else {
		$spotlightVillageTitle = '<strong>' . translate( 'Spotlight' ) . '</strong><br />' . translate( $spotlightVillageTitle ) . ':<br />' . translate( 'indepdendent village' );
	}
	
}

printVillageMap( $spotlightVillageId, $spotlightVillageTitle );
?>

<?php

?>
					<div class="blok">	
						<strong><?php transprint( 'Game information' ); ?></strong><br />
						<ul>
							<li><?php transprint( 'Turn update speed (minutes:seconds)' ); ?>: <?php print( $speedtext ); ?> <?php transprint( 'per turn' ); ?></li>
<?php
if( $row[ 'turn' ] > 1 && $row[ 'turn' ] != $row[ 'max_turn' ] ) {
	$countdown = $row[ 'speed' ] - time() % $row[ 'speed' ];
	$speedtext = ( int ) ( $countdown / 60 ) . ":";
	$countdown = ( $countdown % 60 );
	if( $countdown < 10 ) {
		$countdown = "0" . $countdown;
	}
	$speedtext = $speedtext . $countdown; 						
?>
							<li><?php transprint( 'Next turn update will be in' ); ?>: <?php print( $speedtext ); ?></li>
<?php
}
?>
							<li><?php transprint( 'Current game day is' ); ?>: <?php print( number_format( $row[ 'turn' ] ) ); ?> <?php transprint( 'out of' ); ?> <?php print( number_format( $row[ 'max_turn' ] ) ); ?> <?php transprint( 'days' ); ?></li>
							<li><?php transprint( 'Current game date (dd-mm-yyyy)' ); ?>: <?php print( $date ); ?></li>
							<li><?php transprint( 'Current season' ); ?>: <?php print( $season ); ?></li>
							<li><?php transprint( 'Latest news' ); ?> &quot;<?php print( $history_title ) ?>&quot; <?php transprint( 'on' ); ?> <?php print( $history_date ); ?></li>
						</ul>
<?php
print( '<pre>' . html_entity_decode( stripslashes( $row[ 'server_message' ] ) ) . '</pre>' );
?>
						<br />
						<a href="rounds.php?language=<?php print( $language ); ?>"><strong><?php transprint( 'Round statistics' ); ?></strong></a><br />
						<br />
						<strong><?php transprint( 'Support' ); ?></strong><br />
						<?php transprint( 'Having trouble with the game?' ); ?> <?php transprint( 'See the' ); ?> <a href="support.php?language=<?php print( $language ); ?>">Ages of Strife <?php transprint( 'Support' ); ?></a> <?php transprint( 'page.' ); ?><br />
					</div>
					<br />
					<div class="blok">
						<strong><?php transprint( 'Thanks to all game contributors and bug hunters' ); ?></strong><br />
<?php
//$timing[ 'Before bug and credit' ] = microtime();

$query = "SELECT username " .
		"FROM users " .
		"JOIN medals ON users.user_id = medals.user_id " .
		"WHERE title LIKE 'Research branch insignia' OR title LIKE 'Engineer branch insignia' " .
		"GROUP BY username " .
		"ORDER BY ( SELECT count(medals2.user_id) FROM medals AS medals2 WHERE medals2.user_id = users.user_id AND ( medals2.title LIKE 'Research branch insignia' OR medals2.title LIKE 'Engineer branch insignia' ) ) DESC, username";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$num = mysql_numrows( $result );
$i = 0;
while( $num > $i ) {
	if( $i > 0 ) {
		print( ', ' );
	}  
	print( mysql_result( $result, $i, "username" ) );
	$i ++;
}
?>					
						</div>
						<br />
						<div class="blok">
							<strong><?php transprint( 'And thanks to the translators' ); ?></strong><br />
<?php
//$timing[ 'After bug and credit' ] = microtime();

$query = "SELECT username " .
		"FROM users " .
		"JOIN translations ON users.user_id = translations.user_id " .
		"WHERE approved = 1 AND translated = 1 AND username != 'Zworf' " .
		"GROUP BY username " .
		"ORDER BY RAND()";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$num = mysql_numrows( $result );
$i = 0;
while( $num > $i ) {
	if( $i > 0 ) {
		print( ', ' );
	}  
	print( mysql_result( $result, $i, "username" ) );
	$i ++;
}
/*$timing[ 'End of the page' ] = microtime();
$previous = -1;
foreach( $timing AS $key => $time ) {
	$time = explode( ' ', $time );
	$current = $time[0] + $time[1];
	if( $previous > 0 ) {
		print( "Elapsed $key: " . ( $current - $previous ) . "<br />" );
	}
	else {
		print( "<br /><br />First element: $key<br />" );
	}
	
	$previous = $current;
}*/
?>
					</div>
					<br />
					<div class="blok">
						Ages of Strife <?php transprint( 'is a' ); ?> <a href="http://www.pbbg.org/" target="_blank" title="PBBG"><?php transprint( 'Persistent Browser-Based Game' ); ?></a> <?php transprint( 'and is listed on these sites' ); ?><br />
						<span class="sponsor" style="width: 88px; height: 55px;">
							<a target="_blank" href="http://www.pbbgames.com/site/vote/id/15">
								<img src="http://www.pbbgames.com/images/landpage.jpg" border="0" alt="Vote on PBBGames" style="float: left; margin-top: 29px; margin-right: 5px;" />
							</a><br />
							<a href="http://www.pbbgames.com">
								<img src="http://www.pbbgames.com/images/tracker.gif" border="0" alt="private server" style="float: left; margin-top: -1px;" />
							</a>
						</span>
						<a class="sponsor" href="http://www.free-games.com.au/Detailed/1698.html" target="_blank">
							<img border="0" src="resources/freegames.gif" alt="Free Games" />
						</a>
						<a class="sponsor" href="http://www.topwebgames.com/in.asp?id=3579" target="_blank" >
							<img src="resources/top_web_games.jpg" border="0" alt="Top Web Games" />
						</a> 
						<a class="sponsor" href="http://www.omgspider.com/in.php?game_id=84" target="_blank">
							<img src="resources/omgspider_button.gif" border="0" title="Vote!" alt="Vote!" />
						</a>
						<a href="http://bgtop.net/in.php/1212833691" target="_blank" class="sponsor">
							<img src="resources/bgtop8831.gif" alt="Елате в .: BGtop.net :. Топ класацията на българските сайтове и гласувайте за този сайт!!!" border="0" />
						</a>
						<a href="http://gamelist.bbgsite.com/io/index/id/510.shtml" target="_blank" class="sponsor">
							<img src="resources/gamelist_90X60.gif"  width="90" height="60" />
						</a>
					</div>
				</div>
				<div class="text-center">
					<small>
						<?php transprint( 'Contact' ); ?>: <a href="mailto:contact@agesofstrife.com">contact@agesofstrife.com</a> - <?php transprint( 'Parts of AoS are open source, you can find them' ); ?> <a href="http://opensource.bastiaangrutters.nl"><?php transprint( 'here' ); ?></a>.
					</small>
				</div>
			</div>
		</div>
		<script src="global/yui/yahoo-dom-event/yahoo-dom-event.js"></script>
		<script src="global/yui/animation/animation-min.js"></script>
		<script src="global/yui/container/container-min.js"></script>
		<script src="global/images.js"></script>		
	</body>
</html>
